/**
 * Created by aa on 2016/7/8.
 */
'use strict';
var utils=require('../common/utils');
var querystring = require('querystring');
var cac=utils.cac;

var PAGE_SIZE=10;
exports.RECEIVE_NEWS_LIST = 'RECEIVE_NEWS_LIST';
exports.SET_KEYWORD = 'SET_KEYWORD';
exports.PAGE_SIZE = PAGE_SIZE;

const receiveList = cac('RECEIVE_NEWS_LIST', 'data', 'page');
const setKeyword = cac('SET_KEYWORD', 'value');



exports.fetchList =(keyword, page)=>{
    if(page==undefined){
        page=1;
    }
    return (dispatch, getState) => {
        if(!keyword)
            keyword = getState().news.keyword;
        else
            dispatch(setKeyword(keyword));
        var content= querystring.stringify({ keyword, name: 'topword', page, rows:PAGE_SIZE });

        reqwest({
            url: 'http://www.tngou.net/api/search?'+content,
            type: 'jsonp'
        })
            .then(function (data) {
                if(data.status)
                    dispatch(receiveList(data, page));
            })
    }
};

exports.GET_SINGLE_NEWS='GET_SINGLE_NEWS';

exports.SET_CURRENT_NEWS = 'SET_CURRENT_NEWS';
const setCurrent = cac('SET_CURRENT_NEWS', 'news');
exports.chooseNews = index => (dispatch, getState) => {
    let current = getState().news.list[index];
    dispatch(setCurrent(current))
};

